全部文档

8.2数据解析

提供对指定产品脚本的增删改查功能。

8.2.1新增脚本

功能描述:为物联网平台中的产品创建一个脚本,用于将设备自定义数据解析成平台标准数据。

POST  /api/v1/scripts

Query:

Name Description Required Type
productId 产品ID Yes string
masterKey 产品masterKey No string

Body:

Name Type Description Required
content string 脚本内容 Yes
scriptType string 脚本类型("groovy") Yes

注:创建脚本时输入的产品协议不一样时,content字段(脚本内容)格式可能不一样。

  • 产品协议为MQTT时 脚本内容说明
// content 脚本内容需要提供三个解析方法
// 1. 数据上传数据解析 
byte[] convertDeviceData(byte[] data) {
    return data
}
// 2. 命令下发数据解析 
byte[] serializeCommand(byte[] data) {
    return data
}
// 3. 命令回复数据解析 
byte[] serializeCommandRes(byte[] data) {
    return data
}

Body example:

{
    "scriptType":"groovy",
    "content":"byte[] convertDeviceData(byte[] data){\n    return data\n}\nbyte[] serializeCommand(byte[] data){\n    return data\n}\nbyte[] serializeCommandRes(byte[] data){\n    return data\n}\n"
}

cURL example:

curl --location --request POST '{{address}}/api/v1/scripts?accessKeyId={accessKeyId}&signatureNonce={signatureNonce}&signature={signature}&productId={productId}&masterKey={masterKey}' \
--header 'projectId: {projectId}' \
--header 'Content-Type: application/json' \
--data-raw '{
    "scriptType":"groovy",
    "content":"byte[] convertDeviceData(byte[] data){\n    return data\n}\nbyte[] serializeCommand(byte[] data){\n    return data\n}\nbyte[] serializeCommandRes(byte[] data){\n    return data\n}\n"
}'

Response example:

{
    "code": 0,
    "message": "成功",
    "referInfo": null,
    "data": {
        "productId": 100557,
        "id": 13,
        "content": "byte[] convertDeviceData(byte[] data){\n    return data\n}\nbyte[] serializeCommand(byte[] data){\n    return data\n}\nbyte[] serializeCommandRes(byte[] data){\n    return data\n}\n",
        "requestData": null,
        "dataType": null,
        "response": null,
        "protocol": null
    }
}

  • 产品协议为云网关时 脚本内容说明:
// 脚本内容必须包含两个函数 
// 1. 设备数据上传解析 
import java.nio.ByteBuffer
import groovy.json.*

/**
 * @param data 设备上传数据
 * @return Map 必须包含:
 * length 已处理的长度,
 * model 数据类型: 1表示命令回复,2表示数据上传, 3表示设备发送的心跳包
 * response 返回数据: 命令回复是uuid和属性值,数据上传是属性名和属性值
 */
def parsePubData(byte[] data) {
    def result = [:]
    result.length = 0
    if (data.length < 2) {
        return result
    }

    String symbol = new String(data[0..0] as byte[])
    //测试注解
    if (symbol == "#") //"#" 表示该数据为命令回复
    {
        int length = ByteBuffer.wrap(data[1..1] as byte[]).get()
        if (length < 36) //uuid长度为36位
        {
            return result
        } else {
            if (length + 2 > data.length) {
                return result
            }
            byte[] uuidBytes = data[2..37] as byte[]
            byte[] valueBytes = data[38..38 + length - 36 - 1] as byte[]
            def uuid = new String(uuidBytes)
            def response = [:]
            response.uuid = uuid
            response.identifierValue = new String(valueBytes)
            result.length = length + 2
            result.model = 1
            result.response = response
            return result
        }
    } else if (symbol == "^")// "^" 表示该条数据为设备心跳数据
    {
        if (data.length < 4) {
            return result
        }
        result.length = 4
        result.model = 3
        return result
    }
    else {
        int length = ByteBuffer.wrap(data[0..0] as byte[]).get() * 256 + ByteBuffer.wrap(data[1..1] as byte[]).get()
        if (length + 2 > data.length) {
            return result
        }
        String json = new String(data[2..2 + length - 1] as byte[])
        def jsonSlurper = new JsonSlurper()
        Map response = jsonSlurper.parseText(json)
        result.length = length + 2
        result.model = 2
        result.response = response
        return result
    }
}

/**   解析命令下发到设备
 @param uuid 唯一id
@param key 属性名
@param value 属性值
@param functionType 方法类型,get或者set
@return 下发到设备的数据(json格式)
**/
def parseCommandData(String uuid, String key, Object value, String functionType) {
    def data = [:]
    data.uuid = uuid
    data.identifier = key
    data.functionType = functionType
    if (functionType == "propertySet") {
        data[(key)] = value
    }
    return new JsonBuilder(data).toPrettyString()
}

Body example:

{
    "scriptType":“groovy”,
    "content": "content":"import java.nio.ByteBuffer\n                                            import groovy.json.*\n\n                                            /**\n                                             * @param data 设备上传数据\n                                             * @return Map 必须包含:\n                                             * length 已处理的长度,\n                                             * model 数据类型: 1表示命令回复,2表示数据上传, 3表示设备发送的心跳包\n                                             * response 返回数据: 命令回复是uuid和属性值,数据上传是属性名和属性值\n                                             */\n                                            def parsePubData(byte[] data) {\n                                                def result = [:]\n                                                result.length = 0\n                                                if (data.length < 2) {\n                                                    return result\n                                                }\n\n                                                String symbol = new String(data[0..0] as byte[])\n                                                //测试注解\n                                                if (symbol == \"#\") //\"#\" 表示该数据为命令回复\n                                                {\n                                                    int length = ByteBuffer.wrap(data[1..1] as byte[]).get()\n                                                    if (length < 36) //uuid长度为36位\n                                                    {\n                                                        return result\n                                                    } else {\n                                                        if (length + 2 > data.length) {\n                                                            return result\n                                                        }\n                                                        byte[] uuidBytes = data[2..37] as byte[]\n                                                        byte[] valueBytes = data[38..38 + length - 36 - 1] as byte[]\n                                                        def uuid = new String(uuidBytes)\n                                                        def response = [:]\n                                                        response.uuid = uuid\n                                                        response.identifierValue = new String(valueBytes)\n                                                        result.length = length + 2\n                                                        result.model = 1\n                                                        result.response = response\n                                                        return result\n                                                    }\n                                                } else if (symbol == \"^\")// \"^\" 表示该条数据为设备心跳数据\n                                                {\n                                                    if (data.length < 4) {\n                                                        return result\n                                                    }\n                                                    result.length = 4\n                                                    result.model = 3\n                                                    return result\n                                                }\n                                                else {\n                                                    int length = ByteBuffer.wrap(data[0..0] as byte[]).get() * 256 + ByteBuffer.wrap(data[1..1] as byte[]).get()\n                                                    if (length + 2 > data.length) {\n                                                        return result\n                                                    }\n                                                    String json = new String(data[2..2 + length - 1] as byte[])\n                                                    def jsonSlurper = new JsonSlurper()\n                                                    Map response = jsonSlurper.parseText(json)\n                                                    result.length = length + 2\n                                                    result.model = 2\n                                                    result.response = response\n                                                    return result\n                                                }\n                                            }\n\n                                            /**   解析命令下发到设备\n                                             @param uuid 唯一id\n                                            @param key 属性名\n                                            @param value 属性值\n                                            @param functionType 方法类型,get或者set\n                                            @return 下发到设备的数据(json格式)\n                                            **/\n                                            def parseCommandData(String uuid, String key, Object value, String functionType) {\n                                                def data = [:]\n                                                data.uuid = uuid\n                                                data.identifier = key\n                                                data.functionType = functionType\n                                                if (functionType == \"propertySet\") {\n                                                    data[(key)] = value\n                                                }\n                                                return new JsonBuilder(data).toPrettyString()\n                                            }\n                                            "
}

cURL example:

 curl --location --request POST '{{address}}/api/v1/scripts?accessKeyId={accessKeyId}&signatureNonce={signatureNonce}&signature={signature}&productId={productId}&masterKey={masterKey}' \
 --header 'projectId: {projectId}' \ 
 --header 'Content-Type: application/json' \
 --data-raw '{
     "scriptType":“groovy”,
     "content": "content":"import java.nio.ByteBuffer\n                                            import groovy.json.*\n\n                                            /**\n                                             * @param data 设备上传数据\n                                             * @return Map 必须包含:\n                                             * length 已处理的长度,\n                                             * model 数据类型: 1表示命令回复,2表示数据上传, 3表示设备发送的心跳包\n                                             * response 返回数据: 命令回复是uuid和属性值,数据上传是属性名和属性值\n                                             */\n                                            def parsePubData(byte[] data) {\n                                                def result = [:]\n                                                result.length = 0\n                                                if (data.length < 2) {\n                                                    return result\n                                                }\n\n                                                String symbol = new String(data[0..0] as byte[])\n                                                //测试注解\n                                                if (symbol == \"#\") //\"#\" 表示该数据为命令回复\n                                                {\n                                                    int length = ByteBuffer.wrap(data[1..1] as byte[]).get()\n                                                    if (length < 36) //uuid长度为36位\n                                                    {\n                                                        return result\n                                                    } else {\n                                                        if (length + 2 > data.length) {\n                                                            return result\n                                                        }\n                                                        byte[] uuidBytes = data[2..37] as byte[]\n                                                        byte[] valueBytes = data[38..38 + length - 36 - 1] as byte[]\n                                                        def uuid = new String(uuidBytes)\n                                                        def response = [:]\n                                                        response.uuid = uuid\n                                                        response.identifierValue = new String(valueBytes)\n                                                        result.length = length + 2\n                                                        result.model = 1\n                                                        result.response = response\n                                                        return result\n                                                    }\n                                                } else if (symbol == \"^\")// \"^\" 表示该条数据为设备心跳数据\n                                                {\n                                                    if (data.length < 4) {\n                                                        return result\n                                                    }\n                                                    result.length = 4\n                                                    result.model = 3\n                                                    return result\n                                                }\n                                                else {\n                                                    int length = ByteBuffer.wrap(data[0..0] as byte[]).get() * 256 + ByteBuffer.wrap(data[1..1] as byte[]).get()\n                                                    if (length + 2 > data.length) {\n                                                        return result\n                                                    }\n                                                    String json = new String(data[2..2 + length - 1] as byte[])\n                                                    def jsonSlurper = new JsonSlurper()\n                                                    Map response = jsonSlurper.parseText(json)\n                                                    result.length = length + 2\n                                                    result.model = 2\n                                                    result.response = response\n                                                    return result\n                                                }\n                                            }\n\n                                            /**   解析命令下发到设备\n                                             @param uuid 唯一id\n                                            @param key 属性名\n                                            @param value 属性值\n                                            @param functionType 方法类型,get或者set\n                                            @return 下发到设备的数据(json格式)\n                                            **/\n                                            def parseCommandData(String uuid, String key, Object value, String functionType) {\n                                                def data = [:]\n                                                data.uuid = uuid\n                                                data.identifier = key\n                                                data.functionType = functionType\n                                                if (functionType == \"propertySet\") {\n                                                    data[(key)] = value\n                                                }\n                                                return new JsonBuilder(data).toPrettyString()\n                                            }\n                                            "
 }'

Response example:

{
    "code": 0,
    "message": "成功",
    "referInfo": null,
    "data": {
        "productId": 100573,
        "id": 12,
        "content": "import java.nio.ByteBuffer\n                                            import groovy.json.*\n\n                                            /**\n                                             * @param data 设备上传数据\n                                             * @return Map 必须包含:\n                                             * length 已处理的长度,\n                                             * model 数据类型: 1表示命令回复,2表示数据上传, 3表示设备发送的心跳包\n                                             * response 返回数据: 命令回复是uuid和属性值,数据上传是属性名和属性值\n                                             */\n                                            def parsePubData(byte[] data) {\n                                                def result = [:]\n                                                result.length = 0\n                                                if (data.length < 2) {\n                                                    return result\n                                                }\n\n                                                String symbol = new String(data[0..0] as byte[])\n                                                //测试注解\n                                                if (symbol == \"#\") //\"#\" 表示该数据为命令回复\n                                                {\n                                                    int length = ByteBuffer.wrap(data[1..1] as byte[]).get()\n                                                    if (length < 36) //uuid长度为36位\n                                                    {\n                                                        return result\n                                                    } else {\n                                                        if (length + 2 > data.length) {\n                                                            return result\n                                                        }\n                                                        byte[] uuidBytes = data[2..37] as byte[]\n                                                        byte[] valueBytes = data[38..38 + length - 36 - 1] as byte[]\n                                                        def uuid = new String(uuidBytes)\n                                                        def response = [:]\n                                                        response.uuid = uuid\n                                                        response.identifierValue = new String(valueBytes)\n                                                        result.length = length + 2\n                                                        result.model = 1\n                                                        result.response = response\n                                                        return result\n                                                    }\n                                                } else if (symbol == \"^\")// \"^\" 表示该条数据为设备心跳数据\n                                                {\n                                                    if (data.length < 4) {\n                                                        return result\n                                                    }\n                                                    result.length = 4\n                                                    result.model = 3\n                                                    return result\n                                                }\n                                                else {\n                                                    int length = ByteBuffer.wrap(data[0..0] as byte[]).get() * 256 + ByteBuffer.wrap(data[1..1] as byte[]).get()\n                                                    if (length + 2 > data.length) {\n                                                        return result\n                                                    }\n                                                    String json = new String(data[2..2 + length - 1] as byte[])\n                                                    def jsonSlurper = new JsonSlurper()\n                                                    Map response = jsonSlurper.parseText(json)\n                                                    result.length = length + 2\n                                                    result.model = 2\n                                                    result.response = response\n                                                    return result\n                                                }\n                                            }\n\n                                            /**   解析命令下发到设备\n                                             @param uuid 唯一id\n                                            @param key 属性名\n                                            @param value 属性值\n                                            @param functionType 方法类型,get或者set\n                                            @return 下发到设备的数据(json格式)\n                                            **/\n                                            def parseCommandData(String uuid, String key, Object value, String functionType) {\n                                                def data = [:]\n                                                data.uuid = uuid\n                                                data.identifier = key\n                                                data.functionType = functionType\n                                                if (functionType == \"propertySet\") {\n                                                    data[(key)] = value\n                                                }\n                                                return new JsonBuilder(data).toPrettyString()\n                                            }\n                                            ",
        "requestData": null,
        "dataType": null,
        "response": null,
        "protocol": null
    }
}

8.2.2更新脚本

功能描述:更新物联网平台中的指定产品的某个相关脚本。

PUT  /api/v1/scripts

Query:

Name Description Required Type
productId 产品ID Yes string
masterKey 产品masterKey No string

Body:

Name Type Description Required
content string 脚本内容,与新增格式一致 Yes
id int 脚本id Yes

Body example:

{
    "scriptType":"groovy",
    "content":"import java.nio.ByteBuffer\n                                            import groovy.json.*\n\n                                            /**\n                                             * @param data 设备上传数据\n                                             * @return Map 必须包含:\n                                             * length 已处理的长度,\n                                             * model 数据类型: 1表示命令回复,2表示数据上传, 3表示设备发送的心跳包\n                                             * response 返回数据: 命令回复是uuid和属性值,数据上传是属性名和属性值\n                                             */\n                                            def parsePubData(byte[] data) {\n                                                def result = [:]\n                                                result.length = 0\n                                                if (data.length < 2) {\n                                                    return result\n                                                }\n\n                                                String symbol = new String(data[0..0] as byte[])\n                                                //测试注解\n                                                if (symbol == \"#\") //\"#\" 表示该数据为命令回复\n                                                {\n                                                    int length = ByteBuffer.wrap(data[1..1] as byte[]).get()\n                                                    if (length < 36) //uuid长度为36位\n                                                    {\n                                                        return result\n                                                    } else {\n                                                        if (length + 2 > data.length) {\n                                                            return result\n                                                        }\n                                                        byte[] uuidBytes = data[2..37] as byte[]\n                                                        byte[] valueBytes = data[38..38 + length - 36 - 1] as byte[]\n                                                        def uuid = new String(uuidBytes)\n                                                        def response = [:]\n                                                        response.uuid = uuid\n                                                        response.identifierValue = new String(valueBytes)\n                                                        result.length = length + 2\n                                                        result.model = 1\n                                                        result.response = response\n                                                        return result\n                                                    }\n                                                } else if (symbol == \"^\")// \"^\" 表示该条数据为设备心跳数据\n                                                {\n                                                    if (data.length < 4) {\n                                                        return result\n                                                    }\n                                                    result.length = 4\n                                                    result.model = 3\n                                                    return result\n                                                }\n                                                else {\n                                                    int length = ByteBuffer.wrap(data[0..0] as byte[]).get() * 256 + ByteBuffer.wrap(data[1..1] as byte[]).get()\n                                                    if (length + 2 > data.length) {\n                                                        return result\n                                                    }\n                                                    String json = new String(data[2..2 + length - 1] as byte[])\n                                                    def jsonSlurper = new JsonSlurper()\n                                                    Map response = jsonSlurper.parseText(json)\n                                                    result.length = length + 2\n                                                    result.model = 2\n                                                    result.response = response\n                                                    return result\n                                                }\n                                            }\n\n                                            /**   解析命令下发到设备\n                                             @param uuid 唯一id\n                                            @param key 属性名\n                                            @param value 属性值\n                                            @param functionType 方法类型,get或者set\n                                            @return 下发到设备的数据(json格式)\n                                            **/\n                                            def parseCommandData(String uuid, String key, Object value, String functionType) {\n                                                def data = [:]\n                                                data.uuid = uuid\n                                                data.identifier = key\n                                                data.functionType = functionType\n                                                if (functionType == \"propertySet\") {\n                                                    data[(key)] = value\n                                                }\n                                                return new JsonBuilder(data).toPrettyString()\n                                            }\n                                            "
}

cURL example:

 curl --location --request POST '{{address}}/api/v1/scripts?accessKeyId={accessKeyId}&signatureNonce={signatureNonce}&signature={signature}&productId={productId}&masterKey={masterKey}' \
 --header 'projectId: {projectId}' \
 --header 'Content-Type: application/json' \
 --data-raw '{
     "scriptType":“groovy”,
     "content": "content":"import java.nio.ByteBuffer\n                                            import groovy.json.*\n\n                                            /**\n                                             * @param data 设备上传数据\n                                             * @return Map 必须包含:\n                                             * length 已处理的长度,\n                                             * model 数据类型: 1表示命令回复,2表示数据上传, 3表示设备发送的心跳包\n                                             * response 返回数据: 命令回复是uuid和属性值,数据上传是属性名和属性值\n                                             */\n                                            def parsePubData(byte[] data) {\n                                                def result = [:]\n                                                result.length = 0\n                                                if (data.length < 2) {\n                                                    return result\n                                                }\n\n                                                String symbol = new String(data[0..0] as byte[])\n                                                //测试注解\n                                                if (symbol == \"#\") //\"#\" 表示该数据为命令回复\n                                                {\n                                                    int length = ByteBuffer.wrap(data[1..1] as byte[]).get()\n                                                    if (length < 36) //uuid长度为36位\n                                                    {\n                                                        return result\n                                                    } else {\n                                                        if (length + 2 > data.length) {\n                                                            return result\n                                                        }\n                                                        byte[] uuidBytes = data[2..37] as byte[]\n                                                        byte[] valueBytes = data[38..38 + length - 36 - 1] as byte[]\n                                                        def uuid = new String(uuidBytes)\n                                                        def response = [:]\n                                                        response.uuid = uuid\n                                                        response.identifierValue = new String(valueBytes)\n                                                        result.length = length + 2\n                                                        result.model = 1\n                                                        result.response = response\n                                                        return result\n                                                    }\n                                                } else if (symbol == \"^\")// \"^\" 表示该条数据为设备心跳数据\n                                                {\n                                                    if (data.length < 4) {\n                                                        return result\n                                                    }\n                                                    result.length = 4\n                                                    result.model = 3\n                                                    return result\n                                                }\n                                                else {\n                                                    int length = ByteBuffer.wrap(data[0..0] as byte[]).get() * 256 + ByteBuffer.wrap(data[1..1] as byte[]).get()\n                                                    if (length + 2 > data.length) {\n                                                        return result\n                                                    }\n                                                    String json = new String(data[2..2 + length - 1] as byte[])\n                                                    def jsonSlurper = new JsonSlurper()\n                                                    Map response = jsonSlurper.parseText(json)\n                                                    result.length = length + 2\n                                                    result.model = 2\n                                                    result.response = response\n                                                    return result\n                                                }\n                                            }\n\n                                            /**   解析命令下发到设备\n                                             @param uuid 唯一id\n                                            @param key 属性名\n                                            @param value 属性值\n                                            @param functionType 方法类型,get或者set\n                                            @return 下发到设备的数据(json格式)\n                                            **/\n                                            def parseCommandData(String uuid, String key, Object value, String functionType) {\n                                                def data = [:]\n                                                data.uuid = uuid\n                                                data.identifier = key\n                                                data.functionType = functionType\n                                                if (functionType == \"propertySet\") {\n                                                    data[(key)] = value\n                                                }\n                                                return new JsonBuilder(data).toPrettyString()\n                                            }\n                                            "
 }'

Response example:

{
  "success": true,
  "code": 0,
  "msg": "",
  "data":null
}

8.2.3查询脚本

功能描述:查询物联网平台中的指定产品的某个相关脚本。

GET  /api/v1/scripts

Query:

Name Description Required Type
productId 产品ID Yes string
masterKey 产品masterKey No string

cURL example:

curl --location --request GET '{{address}}/api/v1/scripts?accessKeyId={accessKeyId}&signatureNonce={signatureNonce}&signature={signature}&productId={productId}&masterKey={masterKey}' \
--header 'projectId: {projectId}'

Response Body:

Name Type Description
id int 脚本id
name string 脚本名称
content string 脚本内容,与新增格式一致
created long 脚本创建时间戳
modified string 脚本更新时间戳

Response example:

{
  "success": true,
  "code": 0,
  "msg": "",
  "data": {
    "id": 19,
    "name": "",
    "content": "byte[] convertDeviceData(byte[] data){\n    return data\n}\nbyte[] serializeCommand(byte[] data){\n    return data\n}\nbyte[] serializeCommandRes(byte[] data){\n    return data\n}\n    ",
    "productId": 100197,
    "created": 0,
    "modified": 0
  }
}

8.2.4删除脚本

功能描述:删除物联网平台中的指定产品的某个相关脚本。

DELETE  /api/v1/scripts/{id}

Path:

Name Description Required Type
id 脚本id Yes int

cURL example:

curl --location --request DELETE '{{address}}/api/v1/scripts/{id}?accessKeyId={accessKeyId}&signatureNonce={signatureNonce}&signature={signature}' \
--header 'projectId: {projectId}' \

Response example:

{
  "success": true,
  "code": 0,
  "msg": "",
  "data": 1
}

results matching ""

    No results matching ""